package com.neusoft.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.entity.Admin;
import com.neusoft.service.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	AdminService adminService;

	@RequestMapping("/test")
	@ResponseBody
	public Integer test(Integer id) {
		Admin admin = adminService.loadById(id);
		return admin.getaSex();
	}

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, String account, String password) {
		ModelAndView modelAndView = new ModelAndView();
		Admin admin = adminService.loadByExample(account, password);
		if (admin != null) {
			modelAndView.addObject("loginMessage", "登录成功！");
			modelAndView.addObject("admin", admin);
			request.getSession().setAttribute("admin", admin);
			// 指定视图
			modelAndView.setViewName("admin/books");
		} else {
			modelAndView.addObject("loginMessage", "登录失败！");
			modelAndView.setViewName("admin/login");
		}
		return modelAndView;
	}

	@RequestMapping("/getInformation")
	public ModelAndView getInformation(Integer ID) {
		ModelAndView modelAndView = new ModelAndView("admin////////");
		Admin admin = adminService.loadById(ID);
		modelAndView.addObject("admin", admin);
		return modelAndView;
	}

	@RequestMapping("/modifyPassword")
	public ModelAndView modifyPassword(HttpServletRequest request, String oldPassword, String newPassword,
			String confirm) {
		ModelAndView modelAndView = new ModelAndView("admin/");
		if (oldPassword == null || newPassword == null || confirm == null || oldPassword.trim().isEmpty()
				|| newPassword.trim().isEmpty() || confirm.trim().isEmpty()) {
			modelAndView.addObject("errorMess", "密码不能为空，修改密码失败!");
		} else if (!newPassword.equals(confirm)) {
			modelAndView.addObject("errorMess", "新密码和确认密码不一致，修改密码失败！");
		} else {
			modelAndView.addObject("successMess", "修改成功！");
		}
		HttpSession session = request.getSession();
		Admin admin = (Admin) session.getAttribute("admin");
		admin.setaPassword(newPassword);
		adminService.updateByIdSelective(admin);
		request.getSession().setAttribute("admin", admin);

		return modelAndView;
	}

	/*
	 * @RequestMapping("/addOperator") public ModelAndView addOperator(Operator
	 * operator){ ModelAndView modelAndView = new
	 * ModelAndView("admin/operatorinformation"); //返回值正确吗？？
	 * if(adminService.addOperatorSelective(operator)==1){ ////???
	 * modelAndView.addObject("successMess","增加成功！"); }else{
	 * modelAndView.addObject("errorMess","增加失败！"); } return modelAndView; }
	 */

	@RequestMapping("/updateAdmin")
	public ModelAndView updateAdmin(Admin admin) {
		ModelAndView modelAndView = new ModelAndView("admin/admininformation");
		adminService.updateByIdSelective(admin);
		modelAndView.addObject("admin", admin);
		return modelAndView;
	}

}
